package com.iot08.api.service;

import com.aliyun.tea.utils.StringUtils;
import com.iot08.api.vo.request.SmsNotifyServerAddEditReqVo;
import com.iot08.common.entity.app.SmsNotifyServer;
import com.iot08.common.service.base.BaseAdminService;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

/**
 * @author cwj
 * @description
 * @date 2025/3/16 15:16
 **/
@Slf4j
@Service
public class SmsNotifyServerService extends BaseAdminService<SmsNotifyServer> {

    /**
     * @description 查询短信通知套餐
     * @author cwj
     * @param []
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/3/16 15:33
     **/

    public ResponseMap querySmsNotifyServer(){
        Query query = new Query();
        query.addCriteria(Criteria.where("status").is(1));
        List<SmsNotifyServer> smsNotifyServers = this.find(query);
        ResponseMap res = ResponseMap.success();
        res.put("smsNotifyServers",smsNotifyServers);
        return res;
    }

    /**
     * @description 新增短信通知套餐
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/3/16 16:03
     **/
    public ResponseMap addSmsNotifyServer(SmsNotifyServerAddEditReqVo vo){
        Query query = new Query();
        query.addCriteria(new Criteria().orOperator(Criteria.where("name").is(vo.getName()).and("status").is(1),
                Criteria.where("type").is(vo.getType()).and("status").is(1))).limit(1);
        SmsNotifyServer smsNotifyServer = this.findOne(query);
        if(!ObjectUtils.isEmpty(smsNotifyServer))
        {
            return ResponseMap.fail("已经存在同名短信套餐服务或相同类型的短信套餐服务，无法新增");
        }
        smsNotifyServer = new SmsNotifyServer();
        smsNotifyServer.setName(vo.getName());
        smsNotifyServer.setType(vo.getType());
        smsNotifyServer.setPrice(vo.getPrice());
        smsNotifyServer.setPriceDesc(vo.getPriceDesc());
        smsNotifyServer.setSmsNum(vo.getSmsNum());
        smsNotifyServer.setStatus(vo.getStatus());
        smsNotifyServer.setDesc(vo.getDesc());
        smsNotifyServer.setCreateTime(new Date());
        smsNotifyServer.setUpdateTime(new Date());
        this.save(smsNotifyServer);
        return ResponseMap.success();
    }


    /**
     * @description 编辑短信通知套餐
     * @author cwj
     * @param [vo]
     * @return com.iot08.common.utils.response.ResponseMap
     * @date @time 2025/3/16 16:03
     **/
    public ResponseMap editSmsNotifyServer(SmsNotifyServerAddEditReqVo vo) {
        if(ObjectUtils.isEmpty(vo.getSnId())) {
            return ResponseMap.fail("参数缺失，短信通知服务套餐id不能为空");
        }
        Query query = new Query();
        query.addCriteria(new Criteria().orOperator(Criteria.where("name").is(vo.getName()).and("status").is(1).and("_id").ne(new ObjectId(vo.getSnId())),
                Criteria.where("type").is(vo.getType()).and("status").is(1).and("_id").ne(new ObjectId(vo.getSnId())))).limit(1);
        SmsNotifyServer smsNotifyServer = this.findOne(query);
        if(!ObjectUtils.isEmpty(smsNotifyServer)) {
            return ResponseMap.fail("修改后的短信服务名或类型已经存在于系统中，无法修改");
        }
        Update update = new Update();
        update.set("name",vo.getName());
        update.set("type",vo.getType());
        if(ObjectUtils.isNotEmpty(vo.getPrice())) {
            update.set("price",vo.getPrice());
        }
        if(!StringUtils.isEmpty(vo.getPriceDesc())) {
            update.set("priceDesc",vo.getPriceDesc());
        }
        if(ObjectUtils.isNotEmpty(vo.getSmsNum())) {
            update.set("smsNum",vo.getSmsNum());
        }
        if(ObjectUtils.isNotEmpty(vo.getStatus())) {
            update.set("status",vo.getStatus());
        }if(!StringUtils.isEmpty(vo.getDesc())) {
            update.set("desc",vo.getDesc());
        }
        update.set("update",new Date());
        this.updateById(new ObjectId(vo.getSnId()),update);
        return ResponseMap.success();
    }


}
